//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#if _MSC_VER > 1000
#pragma once
#endif

#ifndef __GHOST_THREAD_H__
#define __GHOST_THREAD_H__

#include <elasys_server.h>
#include <interlocked.h>
#include "ghost.h"

class CGhostThread: public Ghost
{
public:
    UInt32 AddRef();
    UInt32 Release();

public:
    void DoAtException();

public:
    void GetQuitCode(
        /* [out] */ QuitCode * pQuitCode);

    void GetStartTime(
        /* [out] */ SystemTime * pTime);

    void GetQuitTime(
        /* [out] */ SystemTime * pTime);

    void GetName(
        /* [out] */ WStringBuf ** ppName);

    void GetId(
        /* [out] */ ThreadId * pThreadId);

    void DoAtQuit(
        /* [in] */ QuitCode quitCode,
        /* [in] */ SystemTime startTime,
        /* [in] */ SystemTime quitTime,
        /* [in] */ WString name);

public:
    CGhostThread(ThreadId threadId):
                    m_threadId(threadId),
                    m_bAlive(TRUE) {}
    virtual ~CGhostThread() {}
    Boolean IsEqual(ThreadId threadId) {return (m_threadId == threadId);}

public:
    STATIC ECode S_Initialize();
    STATIC void  S_Destroy();

private:
    ThreadId   m_threadId;
    Boolean    m_bAlive;
    QuitCode   m_quitCode;

    SystemTime m_startTime;
    SystemTime m_quitTime;

    WStringBuf_<MAXIMUM_THREAD_NAME_LENGTH> m_name;

private:
    Interlocked  m_nRefs;
};

class CGhostThreadFactory: public GhostFactory
{
public:
    ECode CreateGhost(
        /* [out] */ Ghost ** ppGhost,
        /* [out] */ Boolean * pbExisted);

    ECode GetGhost(
        /* [out] */ Ghost ** ppGhost);

    ECode DeleteGhost(
        /* [in] */ Ghost * pGhost);

public:
    CGhostThreadFactory(ThreadId threadId): m_threadId(threadId) {}

private:
    ThreadId m_threadId;
};

#endif //__GHOST_THREAD_H__

